#coding=utf-8
#Boa:FramePanel:PanelIncomeStatement

import wx
import wx.calendar
import datetime

from product import CDataIncomeStatement
from MyListCtrl import *
import kit

[wxID_PANELINCOMESTATEMENT, wxID_PANELINCOMESTATEMENTBTNDEL, 
 wxID_PANELINCOMESTATEMENTBTNMODIFY, wxID_PANELINCOMESTATEMENTBTNRECALC, 
 wxID_PANELINCOMESTATEMENTBTNSAVE, wxID_PANELINCOMESTATEMENTDATEEND, 
 wxID_PANELINCOMESTATEMENTDATESTART, wxID_PANELINCOMESTATEMENTSTATICTEXT1, 
 wxID_PANELINCOMESTATEMENTSTATICTEXT10, wxID_PANELINCOMESTATEMENTSTATICTEXT11, 
 wxID_PANELINCOMESTATEMENTSTATICTEXT12, wxID_PANELINCOMESTATEMENTSTATICTEXT13, 
 wxID_PANELINCOMESTATEMENTSTATICTEXT14, wxID_PANELINCOMESTATEMENTSTATICTEXT15, 
 wxID_PANELINCOMESTATEMENTSTATICTEXT16, wxID_PANELINCOMESTATEMENTSTATICTEXT17, 
 wxID_PANELINCOMESTATEMENTSTATICTEXT18, wxID_PANELINCOMESTATEMENTSTATICTEXT19, 
 wxID_PANELINCOMESTATEMENTSTATICTEXT2, wxID_PANELINCOMESTATEMENTSTATICTEXT20, 
 wxID_PANELINCOMESTATEMENTSTATICTEXT21, wxID_PANELINCOMESTATEMENTSTATICTEXT22, 
 wxID_PANELINCOMESTATEMENTSTATICTEXT3, wxID_PANELINCOMESTATEMENTSTATICTEXT4, 
 wxID_PANELINCOMESTATEMENTSTATICTEXT5, wxID_PANELINCOMESTATEMENTSTATICTEXT6, 
 wxID_PANELINCOMESTATEMENTSTATICTEXT7, wxID_PANELINCOMESTATEMENTSTATICTEXT8, 
 wxID_PANELINCOMESTATEMENTSTATICTEXT9, wxID_PANELINCOMESTATEMENTTCCOST, 
 wxID_PANELINCOMESTATEMENTTCDISCOUNT, wxID_PANELINCOMESTATEMENTTCEXPENSE, 
 wxID_PANELINCOMESTATEMENTTCGROSSPROFIT, 
 wxID_PANELINCOMESTATEMENTTCINCOMEBEFORETAX, 
 wxID_PANELINCOMESTATEMENTTCINCOMETAX, 
 wxID_PANELINCOMESTATEMENTTCLASTINVENTORIES, wxID_PANELINCOMESTATEMENTTCMEMO, 
 wxID_PANELINCOMESTATEMENTTCNETINCOME, 
 wxID_PANELINCOMESTATEMENTTCNONOPERATINGEXPENSE, 
 wxID_PANELINCOMESTATEMENTTCNONOPERATINGINCOME, 
 wxID_PANELINCOMESTATEMENTTCOPERATINGINCOME, 
 wxID_PANELINCOMESTATEMENTTCOTHERPROFIT, wxID_PANELINCOMESTATEMENTTCPURCHASE, 
 wxID_PANELINCOMESTATEMENTTCSALES, wxID_PANELINCOMESTATEMENTTCSALETAX, 
 wxID_PANELINCOMESTATEMENTTCTHISINVENTORIES, 
] = [wx.NewId() for _init_ctrls in range(46)]

class PanelIncomeStatement(wx.Panel):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, id=wxID_PANELINCOMESTATEMENT,
              name=u'PanelIncomeStatement', parent=prnt, pos=wx.Point(458, 251),
              size=wx.Size(1017, 735), style=wx.TAB_TRAVERSAL)
        self.SetClientSize(wx.Size(1001, 697))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.dateStart = wx.DatePickerCtrl(id=wxID_PANELINCOMESTATEMENTDATESTART,
              name=u'dateStart', parent=self, pos=wx.Point(144, 48),
              size=wx.Size(121, 24), style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY)

        self.staticText1 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT1,
              label=u'\u635f\u76ca\u8868', name='staticText1', parent=self,
              pos=wx.Point(176, 16), size=wx.Size(51, 16), style=0)

        self.staticText2 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT2,
              label=u'\u8d77\u6b62\u65e5\u671f', name='staticText2',
              parent=self, pos=wx.Point(56, 56), size=wx.Size(68, 16), style=0)

        self.staticText3 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT3,
              label=u'\u81f3', name='staticText3', parent=self,
              pos=wx.Point(272, 56), size=wx.Size(17, 16), style=0)

        self.dateEnd = wx.DatePickerCtrl(id=wxID_PANELINCOMESTATEMENTDATEEND,
              name=u'dateEnd', parent=self, pos=wx.Point(304, 48),
              size=wx.Size(121, 24), style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY)

        self.staticText4 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT4,
              label=u'\u4e00\u3001\u4e3b\u8425\u8425\u4e1a\u6536\u5165',
              name='staticText4', parent=self, pos=wx.Point(56, 104),
              size=wx.Size(136, 16), style=0)

        self.tcSales = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCSALES,
              name=u'tcSales', parent=self, pos=wx.Point(224, 96),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcSales.Bind(wx.EVT_TEXT, self.OnTcSalesText,
              id=wxID_PANELINCOMESTATEMENTTCSALES)

        self.staticText5 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT5,
              label=u'\u51cf\uff1a\u6298\u6263', name='staticText5',
              parent=self, pos=wx.Point(80, 136), size=wx.Size(68, 16),
              style=0)

        self.tcDiscount = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCDISCOUNT,
              name=u'tcDiscount', parent=self, pos=wx.Point(224, 128),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcDiscount.Bind(wx.EVT_TEXT, self.OnTcDiscountText,
              id=wxID_PANELINCOMESTATEMENTTCDISCOUNT)

        self.staticText6 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT6,
              label=u'    \u4e3b\u8425\u4e1a\u52a1\u6210\u672c',
              name='staticText6', parent=self, pos=wx.Point(80, 168),
              size=wx.Size(138, 16), style=0)

        self.staticText7 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT7,
              label=u'    \u7a0e\u91d1\u53ca\u9644\u52a0', name='staticText7',
              parent=self, pos=wx.Point(80, 296), size=wx.Size(121, 16),
              style=0)

        self.staticText8 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT8,
              label=u'\u4e8c\u3001\u4e3b\u8425\u4e1a\u52a1\u5229\u6da6',
              name='staticText8', parent=self, pos=wx.Point(56, 328),
              size=wx.Size(136, 16), style=0)

        self.staticText9 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT9,
              label=u'\u52a0\uff1a\u5176\u4ed6\u4e1a\u52a1\u5229\u6da6',
              name='staticText9', parent=self, pos=wx.Point(80, 360),
              size=wx.Size(136, 16), style=0)

        self.staticText10 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT10,
              label=u'\u51cf\uff1a\u8425\u4e1a\u8d39\u7528',
              name='staticText10', parent=self, pos=wx.Point(80, 392),
              size=wx.Size(102, 16), style=0)

        self.staticText13 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT13,
              label=u'\u4e09\u3001\u8425\u4e1a\u5229\u6da6',
              name='staticText13', parent=self, pos=wx.Point(56, 424),
              size=wx.Size(102, 16), style=0)

        self.staticText14 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT14,
              label=u'\u52a0\uff1a\u8425\u4e1a\u5916\u6536\u5165',
              name='staticText14', parent=self, pos=wx.Point(80, 456),
              size=wx.Size(119, 16), style=0)

        self.staticText11 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT11,
              label=u'\u51cf\uff1a\u8425\u4e1a\u5916\u652f\u51fa',
              name='staticText11', parent=self, pos=wx.Point(80, 488),
              size=wx.Size(119, 16), style=0)

        self.staticText12 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT12,
              label=u'\u56db\u3001\u5229\u6da6\u603b\u989d',
              name='staticText12', parent=self, pos=wx.Point(56, 520),
              size=wx.Size(102, 16), style=0)

        self.staticText15 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT15,
              label=u'\u51cf\uff1a\u6240\u5f97\u7a0e', name='staticText15',
              parent=self, pos=wx.Point(80, 552), size=wx.Size(85, 16),
              style=0)

        self.staticText16 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT16,
              label=u'\u4e94\u3001\u51c0\u5229\u6da6', name='staticText16',
              parent=self, pos=wx.Point(56, 584), size=wx.Size(85, 16),
              style=0)

        self.tcCost = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCCOST,
              name=u'tcCost', parent=self, pos=wx.Point(224, 160),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcCost.Bind(wx.EVT_TEXT, self.OnTcCostText,
              id=wxID_PANELINCOMESTATEMENTTCCOST)

        self.tcSaleTax = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCSALETAX,
              name=u'tcSaleTax', parent=self, pos=wx.Point(224, 288),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcSaleTax.SetBackgroundColour(wx.Colour(186, 255, 117))
        self.tcSaleTax.Bind(wx.EVT_TEXT, self.OnTxSaleTaxText,
              id=wxID_PANELINCOMESTATEMENTTCSALETAX)

        self.tcGrossProfit = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCGROSSPROFIT,
              name=u'tcGrossProfit', parent=self, pos=wx.Point(224, 320),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcGrossProfit.Enable(False)
        self.tcGrossProfit.Bind(wx.EVT_TEXT, self.OnTcGrossProfitText,
              id=wxID_PANELINCOMESTATEMENTTCGROSSPROFIT)

        self.tcOtherProfit = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCOTHERPROFIT,
              name=u'tcOtherProfit', parent=self, pos=wx.Point(224, 352),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcOtherProfit.SetBackgroundColour(wx.Colour(186, 255, 117))
        self.tcOtherProfit.Bind(wx.EVT_TEXT, self.OnTcOtherProfitText,
              id=wxID_PANELINCOMESTATEMENTTCOTHERPROFIT)

        self.tcExpense = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCEXPENSE,
              name=u'tcExpense', parent=self, pos=wx.Point(224, 384),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcExpense.Bind(wx.EVT_TEXT, self.OnTcExpenseText,
              id=wxID_PANELINCOMESTATEMENTTCEXPENSE)

        self.tcOperatingIncome = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCOPERATINGINCOME,
              name=u'tcOperatingIncome', parent=self, pos=wx.Point(224, 416),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcOperatingIncome.Enable(False)
        self.tcOperatingIncome.Bind(wx.EVT_TEXT, self.OnTcOperatingIncomeText,
              id=wxID_PANELINCOMESTATEMENTTCOPERATINGINCOME)

        self.tcNonOperatingIncome = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCNONOPERATINGINCOME,
              name=u'tcNonOperatingIncome', parent=self, pos=wx.Point(224, 448),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcNonOperatingIncome.SetBackgroundColour(wx.Colour(186, 255, 117))
        self.tcNonOperatingIncome.Bind(wx.EVT_TEXT,
              self.OnTcNonOperatingIncomeText,
              id=wxID_PANELINCOMESTATEMENTTCNONOPERATINGINCOME)

        self.tcNonOperatingExpense = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCNONOPERATINGEXPENSE,
              name=u'tcNonOperatingExpense', parent=self, pos=wx.Point(224,
              480), size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcNonOperatingExpense.SetBackgroundColour(wx.Colour(186, 255, 117))
        self.tcNonOperatingExpense.Bind(wx.EVT_TEXT,
              self.OnTcNonOperatingExpenseText,
              id=wxID_PANELINCOMESTATEMENTTCNONOPERATINGEXPENSE)

        self.tcIncomeBeforeTax = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCINCOMEBEFORETAX,
              name=u'tcIncomeBeforeTax', parent=self, pos=wx.Point(224, 512),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcIncomeBeforeTax.Enable(False)
        self.tcIncomeBeforeTax.Bind(wx.EVT_TEXT, self.OnTcIncomeBeforeTaxText,
              id=wxID_PANELINCOMESTATEMENTTCINCOMEBEFORETAX)

        self.tcIncomeTax = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCINCOMETAX,
              name=u'tcIncomeTax', parent=self, pos=wx.Point(224, 544),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcIncomeTax.SetBackgroundColour(wx.Colour(186, 255, 117))
        self.tcIncomeTax.Bind(wx.EVT_TEXT, self.OnTcIncomeTaxText,
              id=wxID_PANELINCOMESTATEMENTTCINCOMETAX)

        self.tcNetIncome = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCNETINCOME,
              name=u'tcNetIncome', parent=self, pos=wx.Point(224, 576),
              size=wx.Size(152, 26), style=wx.TE_RIGHT, value=u'')
        self.tcNetIncome.Enable(False)

        self.btnSave = wx.Button(id=wxID_PANELINCOMESTATEMENTBTNSAVE,
              label=u'\u4fdd\u5b58\u65b0\u8868', name=u'btnSave', parent=self,
              pos=wx.Point(768, 48), size=wx.Size(93, 26), style=0)
        self.btnSave.Bind(wx.EVT_BUTTON, self.OnBtnSaveButton,
              id=wxID_PANELINCOMESTATEMENTBTNSAVE)

        self.btnDel = wx.Button(id=wxID_PANELINCOMESTATEMENTBTNDEL,
              label=u'\u5220\u9664', name=u'btnDel', parent=self,
              pos=wx.Point(872, 48), size=wx.Size(93, 26), style=0)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton,
              id=wxID_PANELINCOMESTATEMENTBTNDEL)

        self.btnRecalc = wx.Button(id=wxID_PANELINCOMESTATEMENTBTNRECALC,
              label=u'\u8ba1\u7b97', name=u'btnRecalc', parent=self,
              pos=wx.Point(432, 48), size=wx.Size(93, 26), style=0)
        self.btnRecalc.Bind(wx.EVT_BUTTON, self.OnBtnRecalcButton,
              id=wxID_PANELINCOMESTATEMENTBTNRECALC)

        self.tcMemo = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCMEMO,
              name=u'tcMemo', parent=self, pos=wx.Point(400, 528),
              size=wx.Size(584, 128), style=wx.TE_MULTILINE, value=u'')

        self.btnModify = wx.Button(id=wxID_PANELINCOMESTATEMENTBTNMODIFY,
              label=u'\u4fdd\u5b58\u4fee\u6539', name=u'btnModify', parent=self,
              pos=wx.Point(872, 16), size=wx.Size(93, 26), style=0)
        self.btnModify.Bind(wx.EVT_BUTTON, self.OnBtnModifyButton,
              id=wxID_PANELINCOMESTATEMENTBTNMODIFY)

        self.staticText17 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT17,
              label=u'\u72b6\u6001\u5b8c\u6210\u7684', name='staticText17',
              parent=self, pos=wx.Point(384, 104), size=wx.Size(85, 16),
              style=0)

        self.staticText18 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT18,
              label=u'\u542b\u6295\u8d44\u9879', name='staticText18',
              parent=self, pos=wx.Point(384, 392), size=wx.Size(68, 16),
              style=0)

        self.tcLastInventories = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCLASTINVENTORIES,
              name=u'tcLastInventories', parent=self, pos=wx.Point(240, 192),
              size=wx.Size(136, 26), style=wx.TE_RIGHT, value=u'')
        self.tcLastInventories.SetBackgroundColour(wx.Colour(186, 255, 117))
        self.tcLastInventories.Bind(wx.EVT_TEXT, self.OnTcLastInventoriesText,
              id=wxID_PANELINCOMESTATEMENTTCLASTINVENTORIES)

        self.tcThisInventories = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCTHISINVENTORIES,
              name=u'tcThisInventories', parent=self, pos=wx.Point(240, 256),
              size=wx.Size(136, 26), style=wx.TE_RIGHT, value=u'')
        self.tcThisInventories.Bind(wx.EVT_TEXT, self.OnTcThisInventoriesText,
              id=wxID_PANELINCOMESTATEMENTTCTHISINVENTORIES)

        self.tcPurchase = wx.TextCtrl(id=wxID_PANELINCOMESTATEMENTTCPURCHASE,
              name=u'tcPurchase', parent=self, pos=wx.Point(240, 224),
              size=wx.Size(136, 26), style=wx.TE_RIGHT, value=u'')
        self.tcPurchase.Bind(wx.EVT_TEXT, self.OnTcPurchaseText,
              id=wxID_PANELINCOMESTATEMENTTCPURCHASE)

        self.staticText19 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT19,
              label=u'+ \u4e0a\u671f\u5e93\u5b58', name='staticText19',
              parent=self, pos=wx.Point(144, 200), size=wx.Size(86, 16),
              style=0)

        self.staticText20 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT20,
              label=u'- \u671f\u672b\u5e93\u5b58', name='staticText20',
              parent=self, pos=wx.Point(144, 264), size=wx.Size(86, 16),
              style=0)

        self.staticText21 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT21,
              label=u'+ \u91c7\u8d2d\u6210\u672c', name='staticText21',
              parent=self, pos=wx.Point(144, 232), size=wx.Size(86, 16),
              style=0)

        self.staticText22 = wx.StaticText(id=wxID_PANELINCOMESTATEMENTSTATICTEXT22,
              label=u'\u53ea\u8ba1\u7b97\u5df2\u5165\u5e93\u7684\n\u6ce8\u610f\u6539\u5165\u5e93\u65e5\u671f',
              name='staticText22', parent=self, pos=wx.Point(384, 224),
              size=wx.Size(119, 32), style=0)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.curObj = None

        self.dateStart.SetValue(wx.DateTime.Today())
        self.dateEnd.SetValue(wx.DateTime.Today())

        cols = [(CDataIncomeStatement.fi_id, wx.LIST_FORMAT_LEFT, 100),
                (CDataIncomeStatement.fi_sales, wx.LIST_FORMAT_RIGHT, 120),
                (CDataIncomeStatement.fi_expense, wx.LIST_FORMAT_RIGHT, 80),
                (CDataIncomeStatement.fi_netIncome, wx.LIST_FORMAT_RIGHT, 90)]
                
        #self.listCtrl  = MyListCtrlDecorator(self.lc, cols, CDataCustomerRegion)
        
        self.lc = MySimpleListCtrl(self, CDataIncomeStatement, cols)
        self.lc.Move(wx.Point(550, 96))
        self.lc.SetSize(wx.Size(430, 400))
        #self.lc.refresh()
        self.lc.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnListItemSelected)

        self.btnModify.Enable(False)
        self.btnDel.Enable(False)
        
    def OnListItemSelected(self, event):
        item = self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return

        self.curObj = CDataIncomeStatement.getData({"id":self.lc.GetItemText(item)})
        self.ShowData()

        self.btnModify.Enable(True)
        self.btnDel.Enable(True)
        
    def ShowData(self):
        obj = self.curObj
        
        date = obj.getValue(obj.fi_dateStart)
        #self.dateStart.SetValue(date)
        self.dateStart.SetValue(wx.DateTimeFromDMY(date.day, date.month-1, date.year,0,0,0))
        date = obj.getValue(obj.fi_dateEnd)
        #self.dateEnd.SetValue(date)
        self.dateEnd.SetValue(wx.DateTimeFromDMY(date.day, date.month-1, date.year,0,0,0))

        self.tcSales.SetValue("%.0f" %(obj.getValue(obj.fi_sales)))
        self.tcDiscount.SetValue("%.0f" %(obj.getValue(obj.fi_discount)))
        self.tcCost.SetValue("%.0f" %(obj.getValue(obj.fi_cost)))
        self.tcSaleTax.SetValue("%.0f" %(obj.getValue(obj.fi_saleTax)))
        self.tcGrossProfit.SetValue("%.0f" %(obj.getValue(obj.fi_grossProfit)))
        self.tcOtherProfit.SetValue("%.0f" %(obj.getValue(obj.fi_otherIncome)))
        self.tcExpense.SetValue("%.0f" %(obj.getValue(obj.fi_expense)))
        self.tcOperatingIncome.SetValue("%.0f" %(obj.getValue(obj.fi_operatingIncome)))
        self.tcNonOperatingIncome.SetValue("%.0f" %(obj.getValue(obj.fi_nonOperatingIncome)))
        self.tcNonOperatingExpense.SetValue("%.0f" %(obj.getValue(obj.fi_nonOperatingExpense)))
        self.tcIncomeBeforeTax.SetValue("%.0f" %(obj.getValue(obj.fi_incomeBeforeTax)))
        self.tcIncomeTax.SetValue("%.0f" %(obj.getValue(obj.fi_incomeTax)))
        self.tcNetIncome.SetValue("%.0f" %(obj.getValue(obj.fi_netIncome)))
        self.tcMemo.SetValue(obj.getValue(obj.fi_memo))
        self.tcLastInventories.SetValue("%.0f" %(obj.getValue(obj.fi_lastInventories)))
        self.tcPurchase.SetValue("%.0f" %(obj.getValue(obj.fi_purchase)))
        self.tcThisInventories.SetValue("%.0f" %(obj.getValue(obj.fi_thisInventories)))

    def getData(self):

        data = {
            CDataIncomeStatement.fi_dateStart : kit.str2Date(self.dateStart.GetValue().Format('%Y-%m-%d')),
            CDataIncomeStatement.fi_dateEnd : kit.str2Date(self.dateEnd.GetValue().Format('%Y-%m-%d')),
            CDataIncomeStatement.fi_sales : kit.toFloatOrZero(self.tcSales.GetValue()),
            CDataIncomeStatement.fi_discount : kit.toFloatOrZero(self.tcDiscount.GetValue()),
            CDataIncomeStatement.fi_cost : kit.toFloatOrZero(self.tcCost.GetValue()),
            CDataIncomeStatement.fi_saleTax : kit.toFloatOrZero(self.tcSaleTax.GetValue()),
            CDataIncomeStatement.fi_grossProfit : kit.toFloatOrZero(self.tcGrossProfit.GetValue()),
            CDataIncomeStatement.fi_otherIncome : kit.toFloatOrZero(self.tcOtherProfit.GetValue()),
            CDataIncomeStatement.fi_expense : kit.toFloatOrZero(self.tcExpense.GetValue()),
            CDataIncomeStatement.fi_operatingIncome : kit.toFloatOrZero(self.tcOperatingIncome.GetValue()),
            CDataIncomeStatement.fi_nonOperatingIncome : kit.toFloatOrZero(self.tcNonOperatingIncome.GetValue()),
            CDataIncomeStatement.fi_nonOperatingExpense : kit.toFloatOrZero(self.tcNonOperatingExpense.GetValue()),
            CDataIncomeStatement.fi_incomeBeforeTax : kit.toFloatOrZero(self.tcIncomeBeforeTax.GetValue()),
            CDataIncomeStatement.fi_incomeTax : kit.toFloatOrZero(self.tcIncomeTax.GetValue()),
            CDataIncomeStatement.fi_netIncome : kit.toFloatOrZero(self.tcNetIncome.GetValue()),
            CDataIncomeStatement.fi_memo : self.tcMemo.GetValue(),
            CDataIncomeStatement.fi_lastInventories : kit.toFloatOrZero(self.tcLastInventories.GetValue()),
            CDataIncomeStatement.fi_purchase : kit.toFloatOrZero(self.tcPurchase.GetValue()),
            CDataIncomeStatement.fi_thisInventories : kit.toFloatOrZero(self.tcThisInventories.GetValue()),
        }

        return data

    def OnBtnSaveButton(self, event):
        #if self.curObj is None:
        #    return

        import datetime

        if (self.tcSales.GetValue() == ""):
            return
                
        dlg = wx.TextEntryDialog(self, u'输入表名',
                                u'输入表名',self.dateStart.GetValue().Format('%Y%m%d'),wx.OK|wx.CANCEL|wx.CENTRE)
        if dlg.ShowModal() != wx.ID_OK:
            return
        name = dlg.GetValue()
        
        data = self.getData()
        data[CDataIncomeStatement.fi_id] = name
        
        obj = CDataIncomeStatement.factory()
        obj.setMulFieldVal(data)
        obj.insert()
        self.curObj = obj

        self.lc.refresh()

    def OnBtnDelButton(self, event):
        ''' 删除选中记录 '''
        if self.curObj is not None:
            self.curObj.delete()
            self.lc.refresh()

    def OnBtnRecalcButton(self, event):

        dateStart = self.dateStart.GetValue().Format('%Y-%m-%d')
        dateEnd = self.dateEnd.GetValue().Format('%Y-%m-%d')
        sales,discount = CDataIncomeStatement.GetSales(dateStart, dateEnd)
        self.tcSales.SetValue('%.0f' %(sales or 0))
        self.tcDiscount.SetValue('%.0f' %(discount or 0))
        #self.tcCost.SetValue('%.0f' %(CDataIncomeStatement.GetCost(dateStart, dateEnd)  or 0))
        self.tcPurchase.SetValue('%.0f' %(CDataIncomeStatement.GetPurchase(dateStart, dateEnd)  or 0))
        self.tcThisInventories.SetValue('%.0f' %(CDataAssets.GetInventories()  or 0))
        self.tcExpense.SetValue('%.0f' %(CDataIncomeStatement.GetExpense(dateStart, dateEnd)  or 0))
        

    def RecalcGrossProfit(self):
        ''' 重算主营业务利润 '''
        sales = kit.toFloatOrZero(self.tcSales.GetValue())
        discount = kit.toFloatOrZero(self.tcDiscount.GetValue())
        cost = kit.toFloatOrZero(self.tcCost.GetValue())
        saleTax = kit.toFloatOrZero(self.tcSaleTax.GetValue())
        GrossProfit = sales - discount - cost - saleTax
        self.tcGrossProfit.SetValue('%.0f' %(GrossProfit))

    def OnTcSalesText(self, event):
        self.RecalcGrossProfit()

    def OnTcDiscountText(self, event):
        self.RecalcGrossProfit()

    def OnTcCostText(self, event):
        self.RecalcGrossProfit()

    def OnTxSaleTaxText(self, event):
        self.RecalcGrossProfit()

    def RecalcOperatingIncome(self):
        '''重算营业利润'''
        GrossProfit = kit.toFloatOrZero(self.tcGrossProfit.GetValue())
        OtherProfit = kit.toFloatOrZero(self.tcOtherProfit.GetValue())
        Expense = kit.toFloatOrZero(self.tcExpense.GetValue())
        OperatingIncome = GrossProfit - OtherProfit - Expense
        self.tcOperatingIncome.SetValue('%.0f' %(OperatingIncome))

    def OnTcGrossProfitText(self, event):
        ''''''
        self.RecalcOperatingIncome()

    def OnTcOtherProfitText(self, event):
        self.RecalcOperatingIncome()

    def OnTcExpenseText(self, event):
        self.RecalcOperatingIncome()

    def RecalcIncomeBeforeTax(self):
        '''重算利润总额'''
        OperatingIncome = kit.toFloatOrZero(self.tcOperatingIncome.GetValue())
        NonOperatingIncome = kit.toFloatOrZero(self.tcNonOperatingIncome.GetValue())
        NonOperatingExpense = kit.toFloatOrZero(self.tcNonOperatingExpense.GetValue())
        IncomeBeforeTax = OperatingIncome - NonOperatingIncome - NonOperatingExpense
        self.tcIncomeBeforeTax.SetValue('%.0f' %(IncomeBeforeTax))
        
    def OnTcOperatingIncomeText(self, event):
        self.RecalcIncomeBeforeTax()

    def OnTcNonOperatingIncomeText(self, event):
        self.RecalcIncomeBeforeTax()

    def OnTcNonOperatingExpenseText(self, event):
        self.RecalcIncomeBeforeTax()

    def RecalcNetIncome(self):
        IncomeBeforeTax = kit.toFloatOrZero(self.tcIncomeBeforeTax.GetValue())
        IncomeTax = kit.toFloatOrZero(self.tcIncomeTax.GetValue())
        NetIncome = IncomeBeforeTax - IncomeTax
        self.tcNetIncome.SetValue('%.0f' %(NetIncome))

    def OnTcIncomeBeforeTaxText(self, event):
        self.RecalcNetIncome()

    def OnTcIncomeTaxText(self, event):
        self.RecalcNetIncome()

    def OnBtnModifyButton(self, event):
        ''' 保存修改 '''

        if self.curObj is None:
            return

        dlg = wx.MessageDialog(self, u'是否覆盖原记录？',u'提示',
                                    wx.YES_NO|wx.ICON_QUESTION)
        if(dlg.ShowModal() == wx.ID_NO):
            return

        self.curObj.setMulFieldVal(self.getData())
        self.curObj.update()

    def RecalcCost(self):
        LastInventories = kit.toFloatOrZero(self.tcLastInventories.GetValue())
        ThisInventories = kit.toFloatOrZero(self.tcThisInventories.GetValue())
        Purchase = kit.toFloatOrZero(self.tcPurchase.GetValue())
        Cost = LastInventories + Purchase - ThisInventories
        self.tcCost.SetValue('%.0f' %(Cost))

    def OnTcLastInventoriesText(self, event):
        self.RecalcCost()

    def OnTcThisInventoriesText(self, event):
        self.RecalcCost()

    def OnTcPurchaseText(self, event):
        self.RecalcCost()
